﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using SimulationPresenter.Presentation.Helpers;

namespace SimulationPresenter.Presentation.Primitives
{
    public class ColoredVertexShape : IPresentationPrimitive
    {
        public CustomVertex.PositionColored[] Vertices
        {
            get;
            set;
        }

        public CustomVertex.PositionNormalColored[] VerticesWithNormal
        {
            get;
            set;
        }

        public short[] Indices
        {
            get;
            set;
        }

        private Color color;
        public Color Color
        {
            get
            {
                return color;
            }
            set
            {
                color = value;
                Recolor();
            }
        }

        private void Recolor()
        {
            if (null != Vertices)
            {
                for (int i = 0; i < Vertices.Length; i++)
                {
                    Vertices[i].Color = Color.ToArgb();
                }
            }
            if (null != VerticesWithNormal)
            {
                for (int i = 0; i < VerticesWithNormal.Length; i++)
                {
                    VerticesWithNormal[i].Color = Color.ToArgb();
                }
            }
        }

        public bool Visible
        {
            get;
            set;
        }

        public ColoredVertexShape(IEnumerable<Vector3> verticies, short[] indices, Color color)
        {
            Vertices = verticies.Select(x => new CustomVertex.PositionColored
                                                {
                                                    Position = x
                                                }).ToArray();
            Color = color;
            Indices = indices;
            Visible = true;
        }

        public ColoredVertexShape(Vector3[] verticies, Vector3[] normals, short[] indices, Color color)
        {
            VerticesWithNormal = new CustomVertex.PositionNormalColored[verticies.Length];
            for (int i = 0; i < verticies.Count(); i++)
            {
                VerticesWithNormal[i] = new CustomVertex.PositionNormalColored
                                            {
                                                Position = verticies[i],
                                                Normal = normals[i]
                                            };
            }
            Color = color;
            Indices = indices;
            Visible = true;
        }

        public VertexFormats CustomVertexFormat
        {
            get { return CustomVertex.PositionNormalColored.Format; }
        }

        #region IPresentationPrimitive Members


        public T[] GetVertices<T>() where T: struct
        {
            return VerticesWithNormal.Cast<T>().ToArray();
        }

        public void SetPositions(Vector3[] vectors)
        {
            //MeshExtensions.UpdatePositions(Vertices, vectors, (x, y) => x.Position = y);
        }

        public Vector3[] GetPositions()
        {
            return VerticesWithNormal.Select(x => x.Position).ToArray();
        }

        public float scale
        {
            get
            {
                return 1.0f;
            }
        }

        #endregion

        #region IPresentationPrimitive Members


        public Material Material
        {
            get
            {
                return new Material
                               {
                                   Diffuse = Color
                               };
            }
        }

        public Texture Texture
        {
            get { return null; }
        }

        #endregion

        #region IPresentationPrimitive Members


        public Vector3[] GetPositionsWithScale()
        {
            return GetPositions();
        }

        #endregion

        #region IPresentationPrimitive Members


        public bool UseScale
        {
            get { return false; }
            set { ;}
        }

        #endregion
    }
}
